package com.ruoyi.zjgl.wbzjgl.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.zjgl.wbzjgl.domain.Wbzjgl;
import com.ruoyi.zjgl.wbzjgl.service.IWbzjglService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.List;

/**
 * 外部专家管理Controller
 *
 * @author huacai
 * @date 2024-10-29
 */
@RestController
@RequestMapping("/zjgl/wbzjgl")
public class WbzjglController extends BaseController {
    @Resource
    private IWbzjglService wbzjglService;

    /**
     * 查询外部专家管理列表
     */
    @PreAuthorize("@ss.hasPermi('zjgl:wbzjgl:list')")
    @GetMapping("/list")
    public TableDataInfo list(Wbzjgl wbzjgl) {
        startPage();
        List<Wbzjgl> list = wbzjglService.selectWbzjglList(wbzjgl);
        return getDataTable(list);
    }

    /**
     * 导出外部专家管理列表
     */
    @PreAuthorize("@ss.hasPermi('zjgl:wbzjgl:export')")
    @Log(title = "外部专家管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Wbzjgl wbzjgl) {
        List<Wbzjgl> list = wbzjglService.selectWbzjglList(wbzjgl);
        ExcelUtil<Wbzjgl> util = new ExcelUtil<Wbzjgl>(Wbzjgl.class);
        util.exportExcel(response, list, "外部专家管理数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Wbzjgl> util = new ExcelUtil<Wbzjgl>(Wbzjgl.class);
        util.importTemplateExcel(response, "外部专家管理数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "外部专家管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('zjgl:wbzjgl:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<Wbzjgl> util = new ExcelUtil<Wbzjgl>(Wbzjgl.class);
        InputStream inputStream = file.getInputStream();
        List<Wbzjgl> list = util.importExcel(inputStream);
        inputStream.close();
        int count = wbzjglService.batchInsertWbzjgl(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取外部专家管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('zjgl:wbzjgl:query')")
    @GetMapping(value = "/{wbzjglId}")
    public AjaxResult getInfo(@PathVariable("wbzjglId") String wbzjglId) {
        return success(wbzjglService.selectWbzjglByWbzjglId(wbzjglId));
    }

    /**
     * 新增外部专家管理
     */
    @PreAuthorize("@ss.hasPermi('zjgl:wbzjgl:add')")
    @Log(title = "外部专家管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Wbzjgl wbzjgl) {
        wbzjgl.setUserId(Math.toIntExact(getUserId())); //用户ID
        wbzjgl.setDeptId(Math.toIntExact(getDeptId())); //部门ID
        wbzjgl.setCreateBy(getUsername()); //创建人
        wbzjgl.setIdWhere("存在");
        return toAjax(wbzjglService.insertWbzjgl(wbzjgl));
    }

    /**
     * 修改外部专家管理
     */
    @PreAuthorize("@ss.hasPermi('zjgl:wbzjgl:edit')")
    @Log(title = "外部专家管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Wbzjgl wbzjgl) {
        return toAjax(wbzjglService.updateWbzjgl(wbzjgl));
    }

    /**
     * 删除外部专家管理
     */
    @PreAuthorize("@ss.hasPermi('zjgl:wbzjgl:remove')")
    @Log(title = "外部专家管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{wbzjglIds}")
    public AjaxResult remove(@PathVariable String[] wbzjglIds) {
        return toAjax(wbzjglService.deleteWbzjglByWbzjglIds(wbzjglIds));
    }

    /**
     * 获得外部专家管理主键ID (UUID)
     */
    @GetMapping("/getWbZjGlId")
    public AjaxResult getWbZjGlId(){
        String getWbZjGlId = IdUtils.fastSimpleUUID();
        return success(getWbZjGlId);
    }
}
